package com.haimi.shiguang.business.user.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.haimi.shiguang.R;
import com.haimi.shiguang.api.ApiServeice;
import com.haimi.shiguang.base.BaseFragment;
import com.haimi.shiguang.base.biz.BaseResponse;
import com.haimi.shiguang.base.biz.ResultStatus;
import com.haimi.shiguang.base.dialog.ProgressManager;
import com.haimi.shiguang.bean.response.LoginResponse;
import com.haimi.shiguang.business.main.MainActivity;
import com.haimi.shiguang.framework.Constans;
import com.haimi.shiguang.utils.ButtonClickTimer;
import com.haimi.shiguang.utils.Store;
import com.haimi.shiguang.utils.TitleCallBack;
import com.haimi.shiguang.utils.TitleView;
import com.haimi.shiguang.utils.ToastUtil;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author changsunhaipeng
 * @date 2018/6/17
 */

public class LoginFragment extends BaseFragment {
    public static final String TAG = LoginFragment.class.getSimpleName();
    private EditText mPhone;
    private EditText mPassword;
    private Button mLogin;
    private TitleView mTitle;

    public static LoginFragment newInstance() {
        Bundle args = new Bundle();
        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

    @Override
    public void initView() {
        mPhone = rootView.findViewById(R.id.et_phone);
        mPassword = rootView.findViewById(R.id.et_password);
        mLogin = rootView.findViewById(R.id.btn_login);
        mTitle = rootView.findViewById(R.id.title_view);
        mLogin.setOnClickListener(this);
        mTitle.setLeft("", true, R.drawable.ic_return);
        mTitle.setMiddle("登录");

        String phone = Store.getString(getContextWithinHost(), Constans.PHONE);
        String password = Store.getString(getContextWithinHost(), Constans.PASSWORD);
        if (!TextUtils.isEmpty(phone)) {
            mPhone.setText(phone);
        }
        if (!TextUtils.isEmpty(password)) {
            mPassword.setText(password);
        }
    }

    @Override
    public void initListener() {
        mTitle.setTitleCallBack(new TitleCallBack() {
            @Override
            public void callBackLeft() {
                dismissSelf();
            }

            @Override
            public void callBackRight() {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        if (!ButtonClickTimer.canClick(v)) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            default:
                break;
        }
    }

    /**
     * 登录
     */
    private void login() {
        String phone = mPhone.getText().toString().trim();
        final String password = mPassword.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showToast("手机号不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showToast("密码不能为空");
            return;
        }
        ProgressManager.showProgress(LoginFragment.this);
        Disposable disposable = ApiServeice.getInstance().login(phone, password)
                .subscribe(new Consumer<BaseResponse<LoginResponse>>() {
                    @Override
                    public void accept(BaseResponse<LoginResponse> loginResponse) throws Exception {
                        ProgressManager.closeProgress(LoginFragment.this);
                        if (loginResponse.status.equals(ResultStatus.success)) {
                            ToastUtil.showToast("登陆成功");

                            Store.putString(getContextWithinHost(), Constans.UID, loginResponse.data.uid);
                            Store.putString(getContextWithinHost(), Constans.SESSION_ID, loginResponse.data.sessionId);
                            Store.putString(getContextWithinHost(), Constans.PHONE, loginResponse.data.mobile);
                            Store.putString(getContextWithinHost(), Constans.PASSWORD, password);

                            startActivity(new Intent(getContextWithinHost(), MainActivity.class));
                            getActivityWithinHost().finishCurrent();
                        } else {
                            ToastUtil.showToast(loginResponse.message);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ProgressManager.closeProgress(LoginFragment.this);
                        ToastUtil.showToast("登录失败");
                    }
                });
        compositeDisposable.add(disposable);
    }
}
